package com.zhike.b2b.web;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;

import com.saas.sys.bpm.BpmDefinitionDAO;
import com.saas.sys.bpm.TradeInit;
import com.saas.sys.bpm.TradeRegIntf;
import com.saas.sys.bpm.TradeTypeDAO;
import com.saas.sys.buffer.Buffers;
import com.saas.sys.log.Logger;
import com.zhike.b2b.dao.DbExecute;
import com.zhike.b2b.dao.Dbtable;

@Service("trade")
public class TradeReg  implements TradeRegIntf
{
  @Resource
  DbExecute exec;
  
  Logger log = new Logger(this);
  TradeInit tradeInit;
  String check_trade_type_code = "1901";
  String check_tag = "0";

  public Buffers tradeRegInfo(Buffers paramBuffers)
  {
    log.LOG_INFO("进入主控业务方法...");
    if (paramBuffers.getString("TRADE_TYPE_CODE") == "")
      throw new RuntimeException("业务类型域字段未赋值！");
    Buffers out = new Buffers();
    log.LOG_INFO("********获取业务类型参数开始*********************");
    TradeTypeDAO ttd = new TradeTypeDAO();
    if ((check_tag.equals("1")) || (check_tag == "1"))
      ttd.setParam(":VTRADETYPECODE", check_trade_type_code);
    else
      ttd.setParam(":VTRADETYPECODE", paramBuffers.getString("TRADE_TYPE_CODE"));
    ttd = ttd.selByInfo("SEL_BY_PK");
    if (ttd == null)
      throw new RuntimeException("业务类型未定义【" + paramBuffers.getString("TRADE_TYPE_CODE") + "】！");
    log.LOG_INFO("********获取业务类型参数结束*********************");
    BpmDefinitionDAO bdfd = new BpmDefinitionDAO();
    log.LOG_INFO("********获取业务流程定义开始*********************");
    if ((check_tag.equals("1")) || (check_tag == "1"))
      bdfd.setParam(":VTRADETYPECODE", check_trade_type_code);
    else
      bdfd.setParam(":VTRADETYPECODE", paramBuffers.getString("TRADE_TYPE_CODE"));
    bdfd.setParam(":VRSRV_STR6", "0");
    List bpmDef = bdfd.selByList("SEL_BY_TYPE");
    if (bpmDef == null)
      throw new RuntimeException("业务流程未定义【" + paramBuffers.getString("TRADE_TYPE_CODE") + "】！");
    log.LOG_INFO("********获取业务流程定义结束*********************");
    try
    {
      String str = "";
      Iterator it = bpmDef.iterator();
      while (it.hasNext())
      {
        HashMap localHashMap = (HashMap)it.next();
        Class clz = Class.forName(localHashMap.get("node_class").toString().trim());
        Object obj = clz.newInstance();
        log.LOG_INFO("********输入参数开始*********************");
        paramBuffers.list();
        log.LOG_INFO("********输入参数结束*********************");
        Method mtd = obj.getClass().getDeclaredMethod(localHashMap.get("node_method").toString().trim(), new Class[] { Buffers.class });
        log.LOG_INFO("********[开始执行原子业务方法<" + mtd.getName() + ">]*********************");
        mtd.invoke(obj, new Object[] { paramBuffers });
        log.LOG_INFO("********原子业务方法执行完毕*********************");
        str = clz.getDeclaredField(localHashMap.get("out_query").toString().trim()).getName();
        Dbtable dt = (Dbtable)PropertyUtils.getProperty(obj, str);
        log.LOG_INFO("********获取原子业务方法输出QUERY*********************");
        exec.addTable(dt);
        exec.execBizQuery(paramBuffers);
        log.LOG_INFO("********获取原子业务方法输出BUFFER*********************");
        str = clz.getDeclaredField(localHashMap.get("out_buffer").toString().trim()).getName();
        out = (Buffers)PropertyUtils.getProperty(obj, str);
        log.LOG_INFO("********[结束执行原子业务方法<" + mtd.getName() + ">]*********************");
        log.LOG_INFO("********输出参数开始*********************");
        out.list();
        log.LOG_INFO("********输出参数结束*********************");
        if (out.getInt("RESULT_CODE") == -1)
          break;
        paramBuffers = out;
        mtd = null;
        obj = null;
        clz = null;
      }
      if (out.getInt("RESULT_CODE") != -1)
      {
        out.setString("SUCCEED_FWD", ttd.getSucceed_fwd());
      }
      else
      {
        out.setString("ERROR_FWD", ttd.getError_fwd());
      }
      log.LOG_INFO("--------[全部业务流程处理完毕!]--------");
      log.LOG_INFO("********输出参数开始*********************");
      out.list();
      log.LOG_INFO("********输出参数结束*********************");
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new RuntimeException("调用类未找到：" + localClassNotFoundException);
    }
    catch (InstantiationException localInstantiationException)
    {
      throw new RuntimeException(localInstantiationException);
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      throw new RuntimeException(localIllegalAccessException);
    }
    catch (InvocationTargetException localInvocationTargetException)
    {
      throw new RuntimeException(localInvocationTargetException.getTargetException());
    }
    catch (NoSuchMethodException localNoSuchMethodException)
    {
      throw new RuntimeException("调用方法未找到：" + localNoSuchMethodException);
    }
    catch (NoSuchFieldException localNoSuchFieldException)
    {
      throw new RuntimeException(localNoSuchFieldException);
    }
    finally
    {
      log.LOG_INFO("退出主控业务方法...");
    }
    return out;
  }

  public String getCheck_tag()
  {
    return check_tag;
  }

  public void setCheck_tag(String paramString)
  {
    check_tag = paramString;
  }
}

